const db_promise = require("../config/db.config.js");
const logger = require('../util/logger');

async function createResponse(req, res, next) {
  logger.winston.info('responses.createResponse');

  
    try {
    	
			  const [rows,fields] = await db_promise.query("insert into responses (response_text, action_id, response_type) values (?,?,?)", [req.body.response_text, req.body.action_id, req.body.response_type]);

        console.log(rows);			  
        if (rows.affectedRows >= 1){ 
			      res.status(200).json({ status: 'success', message: 'Updated' });
			  }else{
			  	  res.status(200).json({ status: 'no data', message: 'Updated' });
			  }    


  
    }catch(err){

		    res.status(400).send({
		      message: err
		    });  

	  }   
  
}

async function updateResponse(req, res, next) {
  logger.winston.info('responses.updateResponse');

  
    try {
    	
			  const [rows,fields] = await db_promise.query("update responses set response_text = ?, response_type = ? where response_id = ?", [req.body.response_text, req.body.response_type, req.body.response_id]);

        console.log(rows);			  
        if (rows.affectedRows >= 1){ 
			      res.status(200).json({ status: 'success', message: 'Updated' });
			  }else{
			  	  res.status(200).json({ status: 'no data', message: 'Updated' });
			  }    


  
    }catch(err){

		    res.status(400).send({
		      message: err
		    });  

	  }  
  
}

async function deleteResponse(req, res, next) {
  logger.winston.info('responses.removeResponse');

  
    try {
    	
        const [rows,fields] = await db_promise.query("delete from responses where response_id = ?", [req.body.response_id]);
        console.log(rows);			  
        if (rows.affectedRows >= 1){ 
			      res.status(200).json({ status: 'success', message: 'Updated' });
			  }else{
			  	  res.status(200).json({ status: 'no data', message: 'Updated' });
			  }    


  
    }catch(err){

		    res.status(400).send({
		      message: err
		    });  

	  } 
	    
  
}

module.exports = {
  createResponse,
  deleteResponse,
  updateResponse
};